﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseAutoGrowthLess10percentDBsize : IssueTypeCheckBase
    {
        public DatabaseAutoGrowthLess10percentDBsize()
        {
            IssueTypeId = 3010;
        }

        public override void Check()
        {
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
                Issue issue;
                ThresholdValue thresholdValue = dc.ThresholdValues.FirstOrDefault(t => t.IssueTypeID == IssueTypeId && t.ThresholdValueID == 1);
                var ServerQry = from d in dc.Databases
                                from df in d.DatabaseFiles

                                where df.FileGroup == "PRIMARY"
                                && df.AutoGrowth.Value 
                                &&
                                (
                                    (df.GrowthPercent.HasValue && df.GrowthPercent < thresholdValue.Value)
                                    ||
                                    (df.GrowthMegaBytes.HasValue && (System.Convert.ToDecimal(df.GrowthMegaBytes) / df.SizeInMb.Value) < System.Convert.ToDecimal(0.1))
                                )
                                && (d.DatabaseName != "distribution") && (d.DatabaseName != "master") && (d.DatabaseName != "msdb")
                                && d.SourceDatabaseId == null

                                select df;
                //Get Issuetype object  And Create a new  Global issue object 

                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                foreach (var item in ServerQry)
                {
                    //Get Issuetype object


                    String growthSetting = "";

                    if (item.GrowthPercent.HasValue)
                    {
                        growthSetting = item.GrowthPercent.ToString() + " %";
                    }
                    else if (item.GrowthMegaBytes.HasValue)
                    {
                        growthSetting = item.GrowthMegaBytes.ToString() + " MB";
                    }

                    issue = new Issue();

                    issue.IssueType = issueType;
                    issue.TargetId = item.Database.DatabaseID;
                    issue.Description = issueType.Description.Replace("{Database}", item.Database.DatabaseName).Replace("{X mb}", growthSetting);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }


    }








}
